from django.http import JsonResponse, HttpResponse
from django.shortcuts import render

# Create your views here.

# import time
# from deng import models
# from django.http import JsonResponse
# from rest_framework.views import APIView
#
#
# class AuthView(APIView):
#     def post(self, request, *args, **kwargs):
#         ret = {'code': 1000, 'msg': None}
#         try:
#             # 参数是datadict 形式
#             usr = request.data.get('username')
#             pas = request.data.get('password')
#             print(usr)
#             obj = models.User.objects.filter(username=usr, password=pas).first()
#             print(obj)
#             print(type(obj))
#             print(obj.username)
#             print(obj.password)
#             if not obj:
#                 ret['code'] = '1001'
#                 ret['msg'] = '用户名或者密码错误'
#                 return JsonResponse(ret)
#                 # 里为了简单，应该是进行加密，再加上其他参数
#             token = str(time.time()) + usr
#             print(token)
#             models.userToken.objects.update_or_create(username=obj, defaults={'token': token})
#             ret['msg'] = '登录成功'
#             # ret['token'] = token
#         except Exception as e:
#             ret['code'] = 1002
#             ret['msg'] = '请求异常'
#         return JsonResponse(ret)


# from rest_framework.decorators import api_view
# from django.http import HttpResponse
#
# @api_view(["GET", "POST"])
# def login(request):
#     if request.method == "GET":
#         username = request.GET.get("username")
#         pwd = request.GET.get("password")
#     elif request.method == "POST":
#         username = request.POST.get("username")
#         pwd = request.POST.get("password")
#     else:
#         return render(request, template_name="error.html", context={"msg": "请求方法错误"})
#     if username is not None and pwd is not None:
#         if username == 'admin' and pwd == "admin":
#             return render(request, "home.html", context={"username": username})
#         else:
#             return render(request, "error.html", context={"msg": "用户名和密码错误"})
#
#     else:
#         # return HttpResponse("用户名和密码必填")
#         return render(request, "login.html", context={"msg": "用户名和密码必填"})
from deng.models import StundentInfo


def studentlogin(request):
    if request.method == "GET":
        return render(request, "students.html")
    if request.method == "POST":
        username = request.POST.get("username")
        password = request.POST.get("password")
        print("用户名：{},密码：{}".format(username, password))
        # TODO 获取到对象，从数据库里取值
        res = StundentInfo.objects.get(id=1)
        if res.username == username and res.password == password:
            print("数据库用户名：{},密码：{}".format(res.username, res.password))
            result = {"code": 0,
                      "msg": "success!",
                      "data": {"username": username,
                               "password": password,
                               }
                      }
            return JsonResponse(result)
        elif username.strip() != '' and password.strip() != '':
        # elif username != '' and password != '':
            info = StundentInfo.objects.create(username=username, password=password)
            info.save()
            return HttpResponse("已保存数据")
        else:
            return HttpResponse("请重新输入")